﻿#ifndef DINGDAN_H
#define DINGDAN_H

#include <QObject>
#include "dingdandetail.h"
#include "userrelation.h"

class Dingdan : public QObject
{
    Q_OBJECT
public:
    explicit Dingdan(QObject *parent = nullptr);

    Dingdan(QJsonObject dingdanObj);

    enum DingdanType{
        SaleOut=0, //销售单
        SaleBack=1, //销退单
        PurchaseIn=2, //采购单
        PurchaseBack=3, //采退单
        PurchaseDingdan=4, //采购订单
        TicketAdjust_Client=5, //票款调整-客户
        TicketAdjust_Giver=6 //票款调整-供应商
    };

    enum DingdanState{
        SAVED=0,            //已保存
        WAITFORSALEOUT=1,   //待销售
        WAITFORPURCHASEIN=2,//待采购
        WAITFORSALEBACK=3,  //待销退
        FINISHED            //已完成
    };

    enum FinanceWidgetType{
        TicketOut=5,    //开票
        MoneyIn=6,      //收款
        TicketIn=7,     //收票
        MoneyOut=8      //付款
    };

    Q_ENUM(FinanceWidgetType)

    static QString getFinanceWidgetTypeStr(FinanceWidgetType type){
        switch (type) {
        case FinanceWidgetType::TicketOut:
            return "开票";
        case FinanceWidgetType::MoneyIn:
            return "收款";
        case FinanceWidgetType::TicketIn:
            return "收票";
        case FinanceWidgetType::MoneyOut:
            return "付款";
        }

        return "";
    }

    static QString getFinancePreSaveStr(FinanceWidgetType type){
        switch (type) {
        case FinanceWidgetType::TicketOut:
            return "预开发票";
        case FinanceWidgetType::MoneyIn:
            return "预收货款";
        case FinanceWidgetType::TicketIn:
            return "预收发票";
        case FinanceWidgetType::MoneyOut:
            return "预付货款";
        }

        return "";
    }

    Q_ENUM(DingdanType)
    Q_ENUM(DingdanState)

    int id;
    QString dingdanId;
    QString relatedDingdanId;
    QString sourceUsername;
    QString sourceCompanyId;
    QString targetUsername;
    QString targetCompanyId;
    int state;      //订单状态  0:已保存 1:待销售 2：待采购 3：待销退 5：已完成
    int type;       //订单类型  0:销售单 1:销退单 2:采购单 3:采退单 4:采购订单
    QString date;

    int isTicket;               //是否开票 0:开票 1:不开票
    double sourceFreightPaid;   //我方运费已付
    double sourceFreightUnpaid; //我方运费未付
    double targetFreightPaid;   //对方运费垫付
    double ticket;              //票据金额
    double money;               //货款金额
    double freeTicket;          //免票据金额
    double freeMoney;           //免货款金额

    int totalCount;             //总数量
    double totalMoney;          //总金额
    double ticketIn;            //收票
    double ticketOut;           //开票
    double moneyIn;             //收款
    double moneyOut;            //付款
    double profit;              //利润
    QString remark;             //备注
    QString opNickName;         //操作员姓名

    QList<DingdanDetail*> details;

    void loadFromJsonObject(QJsonObject obj);

    QString toJson();

    QString getStateStr();

    QString getTypeStr();

    void calculateTotal();

    UserRelation* getUserRelation();

    UserRelation* getReverseRelation();

    void copy(Dingdan &dingdan);

    void clear();

signals:

public slots:

private:
    UserRelation *relation;
    UserRelation *reverseRelation;
};

#endif // DINGDAN_H
